package com.quan.configuration;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

import tk.mybatis.spring.annotation.MapperScan;

/**
 * 主业务数据源
 * @author yhaoquan
 *
 */
@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {

	static final String PACKAGE = "com.quan.oa.mapper";

	static final String MAPPER_LOCATION = "classpath:mapper/oa/*.xml";

	@Value("${spring.datasource.db_master.url}")
	private String url;
	@Value("${spring.datasource.db_master.username}")
	private String username;
	@Value("${spring.datasource.db_master.password}")
	private String password;
	@Value("${spring.datasource.db_master.driver-class-name}")
	private String driverClassName;
	
	@Value("${druid.initialSize}")
	private int initialSize;
	@Value("${druid.minIdle}")
	private int minIdle;
	@Value("${druid.maxActive}")
	private int maxActive;
	@Value("${druid.maxWait}")
	private int maxWait;
	@Value("${druid.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	@Value("${druid.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	@Value("${druid.validationQuery}")
	private String validationQuery;
	@Value("${druid.testWhileIdle}")
	private boolean testWhileIdle;
	@Value("${druid.testOnBorrow}")
	private boolean testOnBorrow;
	@Value("${druid.testOnReturn}")
	private boolean testOnReturn;
	@Value("${druid.poolPreparedStatements}")
	private boolean poolPreparedStatements;
	@Value("${druid.maxPoolPreparedStatementPerConnectionSize}")
	private int maxPoolPreparedStatementPerConnectionSize;
	@Value("${druid.filters}")
	private String filters;
	@Value("${druid.connectionProperties}")
	private String connectionProperties;
	@Value("${druid.useGlobalDataSourceStat}")
	private boolean useGlobalDataSourceStat;

	@Primary
	@Bean(name = "masterDataSource")
	public DataSource masterDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		try {
			System.out.println("+=========================="+url);
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
			dataSource.setDriverClassName(driverClassName);
			dataSource.setInitialSize(initialSize);
			dataSource.setMinIdle(minIdle);
			dataSource.setMaxActive(maxActive);
			dataSource.setMaxWait(maxWait);
			dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
			dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
			dataSource.setValidationQuery(validationQuery);
			dataSource.setTestWhileIdle(testWhileIdle);
			dataSource.setTestOnBorrow(testOnBorrow);
			dataSource.setTestOnReturn(testOnReturn);
			dataSource.setPoolPreparedStatements(poolPreparedStatements);
			dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
			dataSource.setConnectionProperties(connectionProperties);
			dataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
			dataSource.setFilters(filters);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return dataSource;
	}

	@Bean(name = "masterTransactionManager")
	@Primary
	public DataSourceTransactionManager masterTransactionManager() {
		return new DataSourceTransactionManager(masterDataSource());
	}

	@Bean(name = "masterSqlSessionFactory")
	@Primary
	public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
			throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(masterDataSource);
		sessionFactory.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));

		return sessionFactory.getObject();
	}

}
